const app = getApp();
const commomUser = require('../../common/commonUser');
var _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qrData:[{
      type:2,
      path:'/images/share_bg.png',
      left:0,
      top:0,
      width:355,
      height:680
    },{
      type:2,
      path: '头像',
      left:14,
      top:20,
      width:50,
      height:50
    },{
      type:3,
      fillText: '名字',
      left:74,
      top:38,
      font:15,
      setFillStyle:"#ffffff"
    },{
      type:2,
      path: '二维码',
      left:245,
      top:520,
      width:80,
      height:80
    },{
      type:3,
      fillText:'扫码进入小程序',
      left:242,
      top:610,
      font:12,
      setFillStyle:"#666"
    }],
    isQr:false,
    avatar:'',
    nickname:'',
    qrcode:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this
    let userInfo = commomUser.getUserInfo();
    _this.setData({
      nickname:userInfo.nickname,
      avatar: userInfo.avatar,
      qrcode: options.url,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.downloadFile({
      url: _this.data.avatar,
      success: function (res) {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          let qrData = _this.data.qrData
          qrData[1].path = res.tempFilePath
          _this.setData({
            qrData
          })
        }
      }
    })
    
    wx.downloadFile({
      url: _this.data.qrcode,
      success: function (res) {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          let qrData = _this.data.qrData
          qrData[2].fillText = _this.data.nickname
          qrData[3].path = res.tempFilePath
          _this.setData({
            qrData
          })
          setTimeout(() => {
            _this.setData({
              isQr: true
            })
          }, 300);
        }
      },
      fail: function (res) {
        console.log(res)
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //保存图片到本地
  baocun: function () {
    let filePath = app.globalData.shareImage;
    wx.saveImageToPhotosAlbum({
      filePath: filePath,
      success(res) {
        wx.showModal({
          content: '图片已保存到相册~',
          showCancel: false,
          confirmText: '好的',
          confirmColor: '#333',
          success: function (res) {
            if (res.confirm) {
              
            }
          }, fail: function (res) {
            wx.showToast({
              title: '取消授权',
              icon: 'none',
              duration: 1500,
            });
          }
        })
      },
      fail(res) {
        if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
          wx.openSetting({
            success(settingdata) {
              console.log(settingdata)
              if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                console.log("获取权限成功，再次点击图片保存到相册")
              } else {
                console.log("获取权限失败")
              }
            }
          })
        }
      }
    })
  },
})